﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;
using System.Collections;
using VietThuongLMS.Utils;
//Author: Kha
namespace VietThuongLMS.Services
{
    public class HocVien_LopService
    {
        public static HocVien_LopViewModel GetHocVien_LopByMaTaiKhoanWithPagination(int id, int pageNum, int pageIndex)
        {
            var context = new LMSDataEntities();
            try
            {
                var hv_llist = (from t in context.TaiKhoans
                                from hv in context.HocViens
                                from hv_l in context.HocVien_Lop
                                where t.ID == id && t.MaLoaiTaiKhoan.Equals("HocVien") && t.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien 
                                select new HocVien_LopMoRongModel { HV_L = hv_l, MonHoc = hv_l.LopHoc.MonHoc.TenMonHoc,
                                                                    TenLopHoc = hv_l.LopHoc.TenLopHoc,
                                                                    TenLoaiLopHoc = hv_l.LoaiLopHoc.TenLoaiLopHoc,
                                                                    GiamGia = 0, HocVienCu = 0,
                                                                    MucHocPhi = hv_l.LoaiLopHoc.HocPhi                                             
                                }).OrderBy(p => p.HV_L.NgayDangKy).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                var hocviencu = (from t in context.TaiKhoans
                                from hv in context.HocViens
                                from hv_l in context.HocVien_Lop
                                where t.ID == id && t.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien && hv_l.DongHocPhi==1 select hv_l).Count();
                if (hocviencu >= 1)
                    hocviencu = 5;
                else
                    hocviencu = 0;
                foreach (HocVien_LopMoRongModel hvmrm in hv_llist)
                {
                    var temp = (from ctkm in context.ChuongTrinhKhuyenMais where ctkm.MaLoaiLopHoc == hvmrm.HV_L.MaLoaiLopHoc select ctkm).Count();
                    if (temp != 0)
                        hvmrm.GiamGia = (int)(from ctkm in context.ChuongTrinhKhuyenMais where ctkm.MaLoaiLopHoc == hvmrm.HV_L.MaLoaiLopHoc select ctkm.GiamGia).First();
                    hvmrm.HocVienCu = hocviencu;
                    hvmrm.TinhHocPhiPhaiDong();
                }
                int total = (from t in context.TaiKhoans
                             from hv in context.HocViens
                             from hv_l in context.HocVien_Lop
                             where t.ID == id && t.MaLoaiTaiKhoan.Equals("HocVien") && t.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                             select hv_l).Count();
                HocVien_LopViewModel hvlmr = new HocVien_LopViewModel(hv_llist, total);
                return hvlmr;
            }
            catch (Exception ex) 
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::GetHocVien_LopByMaTaiKhoanWithPagination", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        public static List<HocVien_Lop> GetHocVien_LopByMaTaiKhoan(int id)
        {
            var context = new LMSDataEntities();
            try
            {
                var hv_llist = (from t in context.TaiKhoans
                                from hv in context.HocViens
                                from hv_l in context.HocVien_Lop
                                where t.ID == id && t.MaLoaiTaiKhoan.Equals("HocVien") && t.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                select hv_l).ToList();
                return hv_llist;
            }
            catch (Exception ex) 
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::GetHocVien_LopByMaTaiKhoan", ex.StackTrace, ex.Message);
                throw ex; 
            }
            finally
            {
                context.Dispose();
            }
        }
        public static bool CapNhatDongHocPhi(int id, int op, string tien)
        {
            var context = new LMSDataEntities();
            try
            {
                HocVien_Lop hv_l = context.HocVien_Lop.First(p => p.ID == id);
                hv_l.DongHocPhi = op;
                hv_l.TienDaDong = tien;
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::CapNhatDongHocPhi", ex.StackTrace, ex.Message);
                throw ex;
                return false;
            }
            finally
            {
                context.Dispose();
            }
            return true;
        }

        /// <summary>
        /// Them hoc vien - lop
        /// | Author: NHDuc
        /// | Created date: 1/12/2013
        /// </summary>
        /// <param name="comp">doi tuong thuoc lop HocVienLop</param>
        /// <returns></returns>
        public static bool InsertHocVienLop(HocVien_Lop comp)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    context.AddToHocVien_Lop(comp);
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::InsertHocVienLop", ex.StackTrace, ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Lay danh sach hocvienlop
        /// | Author: NHDuc
        /// | Created date: 08/12/2013
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static HocVien_LopViewModel GetHocVienLopList(int pageNum, int pageIndex, string userName)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                TaiKhoan tk = context.TaiKhoans.FirstOrDefault(t => t.MaTaiKhoan == userName);
                NhanVien nhanVien = context.NhanViens.FirstOrDefault(n => n.MaTaiKhoan == tk.ID);

                List<HocVien_LopMoRongModel> list = context.HocVien_Lop
                    .Where(h => h.MaLopHoc == null)
                    .Join(context.ChiNhanhs, hl => hl.MaChiNhanh, c => c.ID, (hl, c) => new { HocVien_Lop = hl, ChiNhanh = c })
                    .Join(context.HocViens, hl => hl.HocVien_Lop.MaHocVien, h => h.ID, (hl, h) => new { HocVien_Lop = hl, HocVien = h })
                    .Join(context.TaiKhoans, hv => hv.HocVien.MaTaiKhoan, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = t})
                    .Where(s => s.HocVien_Lop.HocVien_Lop.Deleted == Constant.GENERAL_STATUS_NORMAL
                        && s.HocVien_Lop.ChiNhanh.ID == nhanVien.MaChiNhanh)
                    .OrderByDescending(s => s.HocVien_Lop.HocVien_Lop.NgayDangKy)
                    .Select(a => new HocVien_LopMoRongModel { 
                        HV_L = a.HocVien_Lop.HocVien_Lop, 
                        HoTen = a.TaiKhoan.HoTen,
                        DienThoai = a.TaiKhoan.DienThoai,
                        TenLoaiLopHoc = a.HocVien_Lop.HocVien_Lop.LoaiLopHoc.TenLoaiLopHoc,
                        ChuyenDe = a.HocVien_Lop.HocVien_Lop.ChuyenDe,
                        TinhTrang = a.HocVien_Lop.HocVien_Lop.Deleted.Value,
                        TenChiNhanh = a.HocVien_Lop.ChiNhanh.TenChiNhanh
                    }).ToList();
                return new HocVien_LopViewModel(list.Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList(), list.Count());
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::HocVien_LopViewModel", ex.StackTrace, ex.Message);
                return new HocVien_LopViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static HocVien_LopViewModel GetHocVienLopList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<HocVien_LopMoRongModel> list = context.HocVien_Lop
                    .Where(h => h.MaLopHoc == null)
                    .Join(context.ChiNhanhs, hl => hl.MaChiNhanh, c => c.ID, (hl, c) => new { HocVien_Lop = hl, ChiNhanh = c })
                    .Join(context.HocViens, hl => hl.HocVien_Lop.MaHocVien, h => h.ID, (hl, h) => new { HocVien_Lop = hl, HocVien = h })
                    .Join(context.TaiKhoans, hv => hv.HocVien.MaTaiKhoan, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = t })
                    .Where(s => s.HocVien_Lop.HocVien_Lop.Deleted == Constant.GENERAL_STATUS_NORMAL)
                    .OrderByDescending(s => s.HocVien_Lop.HocVien_Lop.NgayDangKy)
                    .Select(a => new HocVien_LopMoRongModel
                    {
                        HV_L = a.HocVien_Lop.HocVien_Lop,
                        HoTen = a.TaiKhoan.HoTen,
                        DienThoai = a.TaiKhoan.DienThoai,
                        TenLoaiLopHoc = a.HocVien_Lop.HocVien_Lop.LoaiLopHoc.TenLoaiLopHoc,
                        ChuyenDe = a.HocVien_Lop.HocVien_Lop.ChuyenDe,
                        TinhTrang = a.HocVien_Lop.HocVien_Lop.Deleted.Value,
                        TenChiNhanh = a.HocVien_Lop.ChiNhanh.TenChiNhanh
                    }).ToList();
                return new HocVien_LopViewModel(list.Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList(), list.Count());
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::HocVien_LopViewModel", ex.StackTrace, ex.Message);
                return new HocVien_LopViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Lay danh sach hocvienlop
        /// | Author: NHDuc
        /// | Created date: 08/12/2013
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static HocVien_LopViewModel GetHocVienLopList(string userName)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                List<HocVien_LopMoRongModel> list = context.HocVien_Lop
                    .Join(context.HocViens, hl => hl.MaHocVien, h => h.ID, (hl, h) => new { HocVien_Lop = hl, HocVien = h })
                    .Join(context.TaiKhoans, hv => hv.HocVien.MaTaiKhoan, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = t })
                    .Where(s => s.HocVien_Lop.Deleted == Constant.GENERAL_STATUS_NORMAL
                        && s.TaiKhoan.MaTaiKhoan == userName)
                    .OrderByDescending(s => s.HocVien_Lop.NgayDangKy)
                    .Select(a => new HocVien_LopMoRongModel
                    {
                        HV_L = a.HocVien_Lop,
                        HoTen = a.TaiKhoan.HoTen,
                        DienThoai = a.TaiKhoan.DienThoai,
                        TenLoaiLopHoc = a.HocVien_Lop.LoaiLopHoc.TenLoaiLopHoc
                    }).ToList();
                return new HocVien_LopViewModel(list, 0);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::GetHocVienLopList", ex.StackTrace, ex.Message);
                return new HocVien_LopViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Lay hocvienlop theo hoc vien id
        /// | Author: NHDuc
        /// | Created date: 14/12/2013
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static HocVien_Lop GetHocVienLopByHocVienId(int hocVienId)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                HocVien_Lop hl = context.HocVien_Lop.FirstOrDefault(h => h.MaHocVien == hocVienId);
                return hl;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::GetHocVienLopByHocVienId", ex.StackTrace, ex.Message);
                return new HocVien_Lop();
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Lay danh sach hoc vien theo lop
        /// | Author: NHDuc
        /// | Created date: 08/12/2013
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static HocVien_LopViewModel GetHocVienLopByLopIdList(int maLopHoc)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                List<HocVien_LopMoRongModel> list = context.HocVien_Lop
                    .Join(context.HocViens, hl => hl.MaHocVien, h => h.ID, (hl, h) => new { HocVien_Lop = hl, HocVien = h })
                    .Join(context.TaiKhoans, hv => hv.HocVien.MaTaiKhoan, t => t.ID, (s, t) => new { HocVien_Lop = s.HocVien_Lop, HocVien = s.HocVien, TaiKhoan = t })
                    .Where(s => s.HocVien_Lop.Deleted == Constant.GENERAL_STATUS_NORMAL
                        && s.HocVien_Lop.MaLopHoc == maLopHoc)
                    .OrderByDescending(s => s.HocVien_Lop.NgayDangKy)
                    .Select(a => new HocVien_LopMoRongModel
                    {
                        HV_L = a.HocVien_Lop,
                        HoTen = a.TaiKhoan.HoTen,
                        DienThoai = a.TaiKhoan.DienThoai,
                        DiaChi = a.TaiKhoan.DiaChi
                    }).ToList();
                return new HocVien_LopViewModel(list, 0);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::GetHocVienLopByLopIdList", ex.StackTrace, ex.Message);
                return new HocVien_LopViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Sap hoc vien vao lop tuong ung
        /// | Author: NHDuc
        /// | Created Date: 15/12/2013
        /// </summary>
        /// <param name="hvId"></param>
        /// <param name="lopId"></param>
        /// <returns></returns>
        public static bool UpdateHocVienToLop(int hvId, int lopId)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                HocVien_Lop hl = context.HocVien_Lop.FirstOrDefault(h => h.ID == hvId);
                LopHoc lop = context.LopHocs.FirstOrDefault(l => l.ID == lopId);
                if (hl == null || lop == null)
                    return false;
                hl.MaLopHoc = lopId;
                context.SaveChanges();

                //diem danh
                //lay ma phan cong
                List<PhanCong> list = context.PhanCongs.Where(p => p.MaLopHoc == lopId).ToList();
                foreach (PhanCong pc in list)
                {
                    BangDiemDanh bdd = new BangDiemDanh();
                    bdd.MaHocVienLop = hl.ID;
                    bdd.MaPhanCong = pc.ID;
                    bdd.VangMat = Constant.DIEMDANH_CHUADIEMDANH;
                    bdd.LyDo = "";
                    bdd.Ngay = pc.NgayDay;
                    context.AddToBangDiemDanhs(bdd);
                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::UpdateHocVienToLop", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteHocVienLop(int id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                HocVien_Lop obj = context.HocVien_Lop.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    //context.TaiKhoans.DeleteObject(obj);
                    obj.Deleted = -1;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::DeleteHocVienLop", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool UpdateHocVienLopHocPhi(HocVien_Lop comp)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    HocVien_Lop h = context.HocVien_Lop.FirstOrDefault(a => a.ID == comp.ID);
                    h.TienDaDong = comp.TienDaDong;
                    h.LyDo = comp.LyDo;
                    h.DongHocPhi = 1;
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HocVien_LopService::InsertHocVienLopHocPhi", ex.StackTrace, ex.Message);
                return false;
            }
        }
    }
}
